<div class="nav-tabs-custom">
    <div class="tab-content">
        <ul class="nav nav-tabs">
            <li class="active">商品列表</li>
        </ul>
        <div class="tab-pane active" id="tab_2">
            <div class="box box-primary">
                <div class="box-header">
                    <form class="form-horizontal" method="get" action="{{ Request::url() }}" pjax-container>
                        <div class="form-group">

                            <div class="col-sm-3">
                                <input type="text" name="goods_name" value="{{ Request::input('goods_name') }}"
                                       class="form-control" placeholder="请输入商品名称">
                            </div>

                            <div class="col-sm-3">
                                <select name="category" class="form-control">
                                    <option value="0">请选择商品类目</option>
                                    @foreach($categories as $category)
                                        <option value="{{$category->id}}" @if($category->id == Request::input('category')) selected="selected" @endif>{{$category->category_name}} </option>
                                    @endforeach
                                </select>
                            </div>
                            <span class="col-sm-3">
                                 <button type="submit" id="auth-button" class="btn btn-primary">查询</button>
                            </span>
                            <span class="col-sm-3">
                                 <button type="button" id="add_product" class="btn btn-success">添加商品</button>
                            </span>
                        </div>
                    </form>
                </div>
            </div>
            <!--代理统计表格开始-->
            <div class="box">
                <div class="box-body">
                    <table class="table table-bordered table-hover text-center">
                        <thead>
                        <tr>
                            <th>商品编码</th>
                            <th>商品名称</th>
                            <th>商品类目</th>
                            <!-- <th>数值</th> -->
                            <th>创建时间</th>
                            <th>上下架状态</th>
                            <th>操作</th>

                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($goods_list as $_result)
                            <tr>
                                <td>{{ $_result->id }}</td>
                                <td>{{ $_result->product_name }}</td>
                                <td>{{ $_result->category_name }}</td>
                                <td>{{ $_result->created_at }}</td>
                                <td><input target="{{$_result->id}}" name="status" type="checkbox" @if($_result->status==1) checked @endif></td>
                               <td><button onclick="edit_func({{$_result->id}})" class="btn btn-success">编辑</button><button onclick="delete_func({{$_result->id}})" class="btn btn-danger">删除</button></td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    {{ $goods_list->appends(['category' => Request::input("category"),'goods_name'=>Request::input("goods_name")])->links() }}
                </div>
            </div>
        </div>
    </div>
</div>
<link rel="stylesheet" href="/bootstrap_switch/bootstrap_switch.css">
<script type="text/javascript" src="/bootstrap_switch/bootstrap_switch.js" ></script>
<script type="text/javascript">
    $('[name="status"]').bootstrapSwitch({    //初始化按钮
        onText:"上架",
        offText:"下架",
        labelText:'状态',
        labelWith:'38px',
        onColor:"success",
        offColor:"info",
        size:"small",
        onSwitchChange:function(event,state){
            //console.log($(this).attr('target'))
            if(state==true){
                change_status($(this).attr('target'),1)
                console.log("开启");
            }else{
                change_status($(this).attr('target'),0)
                console.log("关闭");
            }
        }
    });

    function change_status(goods_id,status) {
        var index = layer.load()
        //status = status==true?1:0;
        $.ajax({
            url:"{{admin_url('shop/change_products_status')}}",
            data:{
                goods_id:goods_id,
                status:status
            },
            success:function (res) {
                console.log(res);
                layer.close(index)
                if(res.code==0) {
                    var type= status==1?'上架':'下架';
                    layer.msg(type + '成功')
                }else{
                    var type= status==1?'上架':'下架';
                    layer.msg(type + '失败')
                }

            }
        })
    }

    $('#add_product').on('click',function (e) {
        e.preventDefault();
        location.href="{{admin_url('shop/edit_goods')}}";
    })

    function edit_func(id) {
        location.href="/admin/shop/edit_goods?goods_id="+id;
    }

    function delete_func(id) {
        var index = layer.load()
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: "{{admin_url('shop/delete_products')}}",
            // beforeSend: function () {
            //     $(this).attr('disabled', 'disabled');
            // },
            data: {
                goods_id: id,
            },
            success: function (res) {
                layer.close(index)
                if (res.code == 0) {
                    layer.msg('删除成功')
                    location.reload();
                } else {
                    layer.msg('删除失败')
                }
            },
            error: function () {
                self.isLoading = false;
            }
        })
    }
</script>
